package com.itheima.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheim.client.pojo.Address;
import com.itheim.client.vo.AddressVo;
import com.itheima.client.mapper.AddressMapper;
import com.itheima.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author longbosheng
 * @Date 2022/6/21 9:22
 * @PackageName:com.itheima.service
 * @ClassName: AddressService
 * @Description: TODO
 * @Version 1.0
 */
@Service
public class AddressServiceImpl extends ServiceImpl<AddressMapper,Address> implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    /**
     * 地址列表
     * @param id
     * @return
     */
    @Override
    public List<AddressVo> AddressList(String id) {
        return addressMapper.addressList(id);
    }

    /**
     *  按照地址id查询地址信息
     * @param id
     * @return
     */
    @Override
    public AddressVo AddressInfo(String id) {
        AddressVo addressVos = addressMapper.AddressInfo(id);
        return addressVos;
    }

    /**
     * 按照地址id更新地址
     * @param address
     * @return
     */
    @Override
    public int updateAddress(Address address) {
        UpdateWrapper<Address> wrapper = new UpdateWrapper<>();
        wrapper.eq("id", address.getId());
        return addressMapper.update(address,wrapper);
    }

    /**
     * 获取默认地址
     * @param id
     * @return
     */
    @Override
    public AddressVo getDefaultAddress(String id) {
        AddressVo addresses = addressMapper.getDefaultAddress(id);
        return addresses;
    }

    /**
     * 获取默认地址全部信息
     * @param id
     * @return
     */
    @Override
    public Address getDefault(String id) {
        return addressMapper.getDefault(id);
    }

    /**
     * 设置默认地址
     * @param address
     * @return
     */
    @Override
    public int setDefaultAddress(Address address) {
        address.setIsDefault(1);
       return addressMapper.setDefaultAddress(address);
    }


    /**
     * 更新旧默认地址
     * @param defult
     * @return
     */
    @Override
    public int updateDefaultAddress(Address defult) {
        defult.setIsDefault(0);
        return addressMapper.setDefaultAddress(defult);
    }

}
